package com.chwn.harulib.mvp.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chwn.harulib.R;
import com.chwn.harulib.base.BaseFragment;
import com.chwn.harulib.mvp.BasePresenter;
import com.chwn.harulib.mvp.BaseView;
import com.chwn.harulib.mvp.util.TUtil;
import com.chwn.harulib.util.PerfectClickListener;

/**
 * Created by Myshikin on 2017/3/14.
 * desc : 根据数据加载状态显示不同布局
 * 判断是否是首次加载 -- 避免切换页面后重复加载数据(默认显示的Fragment需要设置setLoadDataNow 为 true , 原因参照Fragment生命周期)
 * 需注意数据加载步骤 -- loadData()-->getData()
 */
public abstract class BaseMVPFragment<P extends BasePresenter> extends BaseFragment implements BaseView {

    protected P mPresenter;

    /*Fragment是否可见*/
    private boolean mIsVisible;
    /*Fragment 是否初始化完毕*/
    private boolean mIsPrepared;
    /*Fragment是否是第一次加载数据*/
    protected boolean mIsFirst = true;
    protected LinearLayout mLLErrorView;
    protected LinearLayout mLLProgressView;
    protected LinearLayout mLLContentView;

    private ImageView mErrorView;
    private ImageView mEmptyView;

    private TextView mErrorTV;
    private TextView mEmptyTV;

    protected View mInflateContentView;
    private   View mLLEmptyView;

    public void setLoadDataNow(boolean loadDataNow) {
        this.loadDataNow = loadDataNow;
    }

    public boolean loadDataNow;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initPresent();
    }

    /**
     * 通过反射初始化Presenter、并关联View
     */
    private void initPresent() {
        mPresenter = TUtil.getT(this, 0);
        mPresenter.setV(this);
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            mIsVisible = true;
            onVisible();
        } else {
            mIsVisible = false;
            onInvisible();
        }
    }

    protected void onVisible() {
        loadData();
    }

    protected void onInvisible() {

    }

    @Override
    protected int getRootViewLayout() {
        return R.layout.fragment_base_layout;
    }

    @Override
    protected void initView() {
        super.initView();
        mLLErrorView = (LinearLayout) mRootView.findViewById(R.id.ll_error_retry);
        mLLProgressView = (LinearLayout) mRootView.findViewById(R.id.ll_progress_bar);
        mLLContentView = (LinearLayout) mRootView.findViewById(R.id.ll_content);
        mLLEmptyView = mRootView.findViewById(R.id.ll_empty);
        mErrorView = (ImageView) mRootView.findViewById(R.id.img_err);
        mEmptyView = (ImageView) mRootView.findViewById(R.id.img_empty);
        mErrorTV = (TextView) mRootView.findViewById(R.id.tv_err_desc);
        mEmptyTV = (TextView) mRootView.findViewById(R.id.tv_empty_desc);

        mInflateContentView = LayoutInflater.from(mActivity).inflate(setContentViewId(), null);
        mLLContentView.addView(mInflateContentView);
        initContentView();
    }

    @Override
    protected void initListener() {
        super.initListener();
        mLLErrorView.setOnClickListener(new PerfectClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                showLoadingView();
                getData();
            }
        });
    }

    protected abstract int setContentViewId();

    protected abstract void initContentView();

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        showLoadingView();
        mIsPrepared = true;
        if (loadDataNow) {
            mIsVisible = true;
            loadData();
        }
    }

    @Override
    public void loadData() {
        super.loadData();
        if (!mIsPrepared || !mIsVisible || !mIsFirst) {
            return;
        }
        getData();
    }

    /**
     * 加载数据
     */
    protected abstract void getData();

    @Override
    public void onDestroy() {
        mPresenter.onDestroy();
        super.onDestroy();
    }

    @Override
    public void onRequestStart() {

    }

    @Override
    public void onRequestError(Throwable e) {

    }

    @Override
    public void onRequestSuccess() {

    }

    public void showContentView() {
        mLLErrorView.setVisibility(View.GONE);
        mLLProgressView.setVisibility(View.GONE);
        mLLContentView.setVisibility(View.VISIBLE);
        mLLEmptyView.setVisibility(View.GONE);
    }


    public void showLoadingView() {
        mLLErrorView.setVisibility(View.GONE);
        mLLProgressView.setVisibility(View.VISIBLE);
        mLLContentView.setVisibility(View.GONE);
        mLLEmptyView.setVisibility(View.GONE);
    }

    /**
     * 此方法没必要 , 后期去掉
     * 看来还是有必要的 :(
     */
    @Override
    public void showEmptyView() {
        mLLErrorView.setVisibility(View.GONE);
        mLLProgressView.setVisibility(View.GONE);
        mLLContentView.setVisibility(View.GONE);
        mLLEmptyView.setVisibility(View.VISIBLE);
    }

    @Override
    public void showErrorView() {
        mLLErrorView.setVisibility(View.VISIBLE);
        mLLProgressView.setVisibility(View.GONE);
        mLLContentView.setVisibility(View.GONE);
        mLLEmptyView.setVisibility(View.GONE);
    }

    protected void setIconSex(int sex){
        if (sex == 0) {
            setErrorView(R.drawable.icon_reload_girl);
            setEmptyView(R.drawable.icon_empty_girl);
        }else{
            setErrorView(R.drawable.icon_reload_boy);
            setEmptyView(R.drawable.icon_empty_boy);
        }
    }

    protected void setErrorView(int resId) {
        mErrorView.setImageResource(resId);
    }

    protected void setEmptyView(int resId) {
        mEmptyView.setImageResource(resId);
    }

    protected void setErrorText(String text) {
        mErrorTV.setText(text);
    }

    protected void setEmptyText(String text) {
        mEmptyTV.setText(text);
    }
}
